﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OutlookTags.Helpers;
using OutlookTags.Core.Controls;
using OutlookTags.Native;

namespace OutlookTags.Controls
{
    public class CommandBarEditAutocomplete : CommandBarEdit
    {
        Form _autoCompleteForm;
        CategoryAutocompleteControl _autoCompleteControl = new CategoryAutocompleteControl();

        public bool AutocompleteEnabled { get; set; }
        public AutoCompleteListSource AutoCompleteSource { get; private set; }
        public event EventHandler<DoCompleteEventArgs> DoComplete;
        
        public CommandBarEditAutocomplete(Microsoft.Office.Core.CommandBarControl control)
            :base(control)
        {
            this.Leave += new EventHandler(CommandBarEditAutocomplete_Leave);
            this.Char += new EventHandler(CommandBarEditAutocomplete_Char);

            AutoCompleteSource = new AutoCompleteListSource();
            DoComplete += new EventHandler<DoCompleteEventArgs>(CommandBarEditAutocomplete_DoComplete);
        }
        

        void CommandBarEditAutocomplete_Char(object sender, EventArgs e)
        {
            var text = this.GetText();
            string prefix = TextFormatHelper.GetCurrentTagPrefix(text);
            AutoCompleteSource.SetPrefix(prefix);
            _autoCompleteControl.RefresAutoCompleteSource();
        }

        void CommandBarEditAutocomplete_Leave(object sender, EventArgs e)
        {            
            ReleaseAutoCompleteForm();
        }

        protected override bool OnKeyDown(Keys key)
        {
            if (_autoCompleteForm == null)
                        return true;

            switch (key)
            {
                case Keys.Tab:
                    OnDoComplete(_autoCompleteControl.SelectedText);
                    return false;
                case Keys.Up:
                    _autoCompleteControl.Up();
                    break;
                case Keys.Down:
                    _autoCompleteControl.Down();
                    break;
            }

            return base.OnKeyDown(key);
        }

        protected override bool OnKeyUp(Keys key)
        {                           
            var res = base.OnKeyUp(key);
            if (Keys.Tab != key)
                EnsureAutocompleteFormVisible();

            return res;
        }

        private void EnsureAutocompleteFormVisible()
        {
            if (_autoCompleteForm == null)
            {
                _autoCompleteForm = new Form();
                
                _autoCompleteForm.FormBorderStyle = FormBorderStyle.None;
                _autoCompleteForm.ShowInTaskbar = false;
                _autoCompleteControl.Dock = DockStyle.Fill;
                _autoCompleteForm.Controls.Add(_autoCompleteControl);                
                _autoCompleteControl.AssignAutoCompleteSource(AutoCompleteSource);

                _autoCompleteForm.Width = Control.Width;
                _autoCompleteForm.Height = 100;//TODO: calculate height based on contant
                _autoCompleteForm.Top = Control.Top + Control.Height;
                _autoCompleteForm.Left = Control.Left;
                
                SafeNativeMethods.ShowInactiveTopmost(_autoCompleteForm);                
            }            
            
        }

        private void ReleaseAutoCompleteForm()
        {
            if (_autoCompleteForm != null && _autoCompleteForm.Visible)
            {
                _autoCompleteForm.Controls.Remove(_autoCompleteControl);
                _autoCompleteForm.Close();
                _autoCompleteForm = null;
            }
        }

        private void OnDoComplete(string completeText)
        {
            EventHandler<DoCompleteEventArgs> tmp = DoComplete;
            if (tmp != null)
            {
                tmp(this, new DoCompleteEventArgs(completeText));
            }
        }

        void CommandBarEditAutocomplete_DoComplete(object sender, DoCompleteEventArgs e)
        {            
            string text = TextFormatHelper.ReplaceLast(this.Text, e.CompleteText);

            CHARRANGE range = new CHARRANGE() { cpMax = text.Length, cpMin = text.Length};
            this.Text = text;

            SetSelectRange(range);

            ReleaseAutoCompleteForm();
        }
    }
}
